﻿using System;
using System.Text;

namespace EXAM.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        string _sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["Username"] != null)
            //{
            //    Response.Redirect("~/Admin/Page_Admin/Categories.aspx");
            //}
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //Response.Redirect("");
            BUS.UserControl usrctr = new BUS.UserControl();
            DTO.UserInfo usrinf = new DTO.UserInfo();
            usrinf = usrctr.Get_User_ByUsername(txtUsername.Text.Trim(), ref _sErr);
            String passcod = Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(txtPassword.Text)));
            if (usrinf.ID > 0 && usrinf.Password == passcod && usrinf.ANAL_U9 == "True")
            {
                Session["UserAdmin"] = usrinf;
                Response.Redirect("~/Admin/Page_Admin/Home.aspx");
            }
            else
            {
                lblErr.Text = "Not correct !!!";
            }

            //AdminInfo CheckA = new AdminInfo();
            //AdminControl adminControl = new AdminControl();
            //if (this.txtUsername.Text != "" && this.txtPassword.Text != "")
            //{
            //    CheckA.Username = this.txtUsername.Text.ToString().Trim();
            //    CheckA.Password = this.txtPassword.Text.ToString().Trim();
            //    try
            //    {
            //        if (adminControl.CheckLogin(CheckA.Username, CheckA.Password) != 0)
            //        {
            //            Session["Username"] = this.txtUsername.Text;
            //            Response.Redirect("~/Admin/Page_Admin/Categories.aspx");
            //        }
            //        else
            //        {
            //            lblErr.Text = "Kiểm tra lai UserName & Password";
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        lblErr.Text = ex.Message;
            //    }

            //}
            //else
            //{
            //    lblErr.Text = "Đăng nhập lại không đươc để trống";
            //}
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtUsername.Text = "";
            this.txtPassword.Text = "";
        }
    }
}